package com.java.sevice.impl;

import com.java.dto.ResultMsg;
import com.java.entity.UserAccount;
import com.java.repository.UserAccountRespository;
import com.java.sevice.ITransferService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
@Slf4j
public class TransferService implements ITransferService {
    @Autowired
    private UserAccountRespository userAccountRespository;

    @Override
    @Transactional
    public ResultMsg doTransfer(Long accountId, BigDecimal amount) {
        log.info("bank1-doTransfer accountId:{} amount:{}", accountId, amount);
        UserAccount userAccount = this.userAccountRespository.getOne(accountId);
        log.info("bank1初始余额:{} ", userAccount.getAmount());
        BigDecimal updateAmount = userAccount.getAmount().add(amount);
        this.userAccountRespository.updateAmount(accountId, updateAmount);
        log.info("bank1加钱成功 金额:{}", amount);
        return ResultMsg.ok("ok");
    }

}